package cn.stylefeng.guns.modular.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.biz.entity.WmMdWarehouse;
import cn.stylefeng.guns.modular.biz.enums.WmMdWarehouseExceptionEnum;
import cn.stylefeng.guns.modular.biz.mapper.WmMdWarehouseMapper;
import cn.stylefeng.guns.modular.biz.pojo.request.WmMdWarehouseRequest;
import cn.stylefeng.guns.modular.biz.service.WmMdWarehouseService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Date;
import java.math.BigDecimal;

/**
 * 煤场基础场地代码业务实现层
 *
 * @author 张朋飞
 * @since 2024/09/13 16:37
 */
@Service
public class WmMdWarehouseServiceImpl extends ServiceImpl<WmMdWarehouseMapper, WmMdWarehouse> implements WmMdWarehouseService {

	@Override
    public void add(WmMdWarehouseRequest wmMdWarehouseRequest) {
        WmMdWarehouse wmMdWarehouse = new WmMdWarehouse();
        BeanUtil.copyProperties(wmMdWarehouseRequest, wmMdWarehouse);
        this.save(wmMdWarehouse);
    }

    @Override
    public void del(WmMdWarehouseRequest wmMdWarehouseRequest) {
        WmMdWarehouse wmMdWarehouse = this.queryWmMdWarehouse(wmMdWarehouseRequest);
        this.removeById(wmMdWarehouse.getSiteId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchDelete(WmMdWarehouseRequest wmMdWarehouseRequest) {
        this.removeByIds(wmMdWarehouseRequest.getBatchDeleteIdList());
    }

    @Override
    public void edit(WmMdWarehouseRequest wmMdWarehouseRequest) {
        WmMdWarehouse wmMdWarehouse = this.queryWmMdWarehouse(wmMdWarehouseRequest);
        BeanUtil.copyProperties(wmMdWarehouseRequest, wmMdWarehouse);
        this.updateById(wmMdWarehouse);
    }

    @Override
    public WmMdWarehouse detail(WmMdWarehouseRequest wmMdWarehouseRequest) {
        return this.queryWmMdWarehouse(wmMdWarehouseRequest);
    }

    @Override
    public PageResult<WmMdWarehouse> findPage(WmMdWarehouseRequest wmMdWarehouseRequest) {
        LambdaQueryWrapper<WmMdWarehouse> wrapper = createWrapper(wmMdWarehouseRequest);
        Page<WmMdWarehouse> pageList = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(pageList);
    }

    @Override
    public List<WmMdWarehouse> findList(WmMdWarehouseRequest wmMdWarehouseRequest) {
        LambdaQueryWrapper<WmMdWarehouse> wrapper = this.createWrapper(wmMdWarehouseRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private WmMdWarehouse queryWmMdWarehouse(WmMdWarehouseRequest wmMdWarehouseRequest) {
        WmMdWarehouse wmMdWarehouse = this.getById(wmMdWarehouseRequest.getSiteId());
        if (ObjectUtil.isEmpty(wmMdWarehouse)) {
            throw new ServiceException(WmMdWarehouseExceptionEnum.WM_MD_WAREHOUSE_NOT_EXISTED);
        }
        return wmMdWarehouse;
    }

    /**
     * 创建查询wrapper
     *
     * @author 张朋飞
     * @since 2024/09/13 16:37
     */
    private LambdaQueryWrapper<WmMdWarehouse> createWrapper(WmMdWarehouseRequest wmMdWarehouseRequest) {
        LambdaQueryWrapper<WmMdWarehouse> queryWrapper = new LambdaQueryWrapper<>();

        Long siteId = wmMdWarehouseRequest.getSiteId();
        queryWrapper.eq(ObjectUtil.isNotNull(siteId), WmMdWarehouse::getSiteId, siteId);

        String siteName = wmMdWarehouseRequest.getSiteName();
        queryWrapper.like(ObjectUtil.isNotEmpty(siteName), WmMdWarehouse::getSiteName, siteName);

        String siteCode = wmMdWarehouseRequest.getSiteCode();
        queryWrapper.like(ObjectUtil.isNotEmpty(siteCode), WmMdWarehouse::getSiteCode, siteCode);

        BigDecimal siteIdealCapacity = wmMdWarehouseRequest.getSiteIdealCapacity();
        queryWrapper.eq(ObjectUtil.isNotNull(siteIdealCapacity), WmMdWarehouse::getSiteIdealCapacity, siteIdealCapacity);

        BigDecimal siteLowStock = wmMdWarehouseRequest.getSiteLowStock();
        queryWrapper.eq(ObjectUtil.isNotNull(siteLowStock), WmMdWarehouse::getSiteLowStock, siteLowStock);

        String remark = wmMdWarehouseRequest.getRemark();
        queryWrapper.like(ObjectUtil.isNotEmpty(remark), WmMdWarehouse::getRemark, remark);


        return queryWrapper;
    }

}
